# Contributor: Alexandru Campeanu <tiotags1@gmail.com>
# Maintainer: Alexandru Campeanu <tiotags1@gmail.com>
pkgname=hinsightd
pkgver=0.9.15
pkgrel=0
pkgdesc="hinsightd a http/1.1 webserver with (hopefully) minimal goals"
url="https://tiotags.gitlab.io/hinsightd"
arch="all"
license="BSD-3-Clause"
makedepends="cmake libcap lua-dev openssl-dev zlib-dev liburing-dev linux-headers"
options="!check" # no test suite
pkgusers="$pkgname"
pkggroups="$pkgname"
subpackages="$pkgname-dbg $pkgname-openrc"
source="
	https://gitlab.com/tiotags/hin9/-/archive/v$pkgver/hin9-v$pkgver.tar.gz
	compile.patch
	"
builddir="$srcdir"/hin9-v$pkgver

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=None \
		-DUSE_FCGI=1 \
		-DUSE_OPENSSL=1 \
		-DUSE_RPROXY=1 \
		$CMAKE_CROSSOPTS .
	cmake --build build
}

package() {
	install -D -m755 "$builddir"/build/hin9 \
		"$pkgdir"/usr/sbin/hinsightd
	setcap cap_net_bind_service+eip "$pkgdir"/usr/sbin/hinsightd
	install -D -m755 "$builddir"/build/hin9_pid_helper \
		"$pkgdir"/usr/bin/hin9_pid_helper

	# create dirs
	install -d -m2750 -o $pkgusers -g $pkggroups \
		"$pkgdir"/var/log/hinsightd/
	install -d -m755 \
		"$pkgdir"/etc/hinsightd/ \
		"$pkgdir"/etc/hinsightd/config/ \
		"$pkgdir"/var/www/localhost/htdocs

	install -m755 -D "$builddir"/external/packaging/$pkgname.initd.sh \
		"$pkgdir"/etc/init.d/$pkgname
	install -m644 -D "$builddir"/external/packaging/$pkgname.confd.sh \
		"$pkgdir"/etc/conf.d/$pkgname
	install -m644 -D "$builddir"/external/packaging/$pkgname.logrotate.sh \
		"$pkgdir"/etc/logrotate.d/$pkgname

	# config files
	local i; for i in main.lua lib.lua
	do
		install -m644 "$builddir"/workdir/"$i" "$pkgdir"/etc/$pkgname/"$i"
	done
	install -m644 -D "$builddir"/workdir/config/* -t "$pkgdir"/etc/$pkgname/config/

}

sha512sums="
1bc926fa4ddedd794e7cc5e781ff98f4dd20b64c6b85cb3a42dcfc04516741424e0dbb280edf7f89ff26ccef6b678df5155be225f10c9e9bc03f8dbca37ac321  hin9-v0.9.15.tar.gz
04bb68392bbf470a928c776dcb3d4d326c3f2c1a7c886ef2749f3f86b10c24b9af17d97d608ffb2bd8e90275260bd312f01bfa67515c248216bbf48601541fe6  compile.patch
"
